package com.example.sicnuaccountingapp.utils

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateDecelerateInterpolator

object ViewUtils {
    fun showLoading(parent: ViewGroup, loadingView: View) {
        if (loadingView.parent == null) {
            parent.addView(loadingView)
            ObjectAnimator.ofFloat(loadingView, "alpha", 0f, 1f).apply {
                duration = 300
                interpolator = AccelerateDecelerateInterpolator()
                start()
            }
        }
    }

    fun hideLoading(loadingView: View) {
        if (loadingView.parent != null) {
            ObjectAnimator.ofFloat(loadingView, "alpha", 1f, 0f).apply {
                duration = 300
                interpolator = AccelerateDecelerateInterpolator()
                addListener(object : AnimatorListenerAdapter() {
                    override fun onAnimationEnd(animation: Animator) {
                        (loadingView.parent as? ViewGroup)?.removeView(loadingView)
                    }
                })
                start()
            }
        }
    }
} 